<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>数据库表管理</title>
    <link rel="stylesheet" type="text/css" href="../biz/ext3/examples/ux/gridfilters/css/GridFilters.css" />
    <link rel="stylesheet" type="text/css" href="../biz/ext3/examples/ux/gridfilters/css/RangeMenu.css" />
    <link rel="stylesheet" type="text/css" href="../biz/ext3/resources/css/ext-all.css" />
    <link rel="stylesheet" type="text/css" href="../biz/ext3/examples/ux/css/MultiSelect.css" />
    <link rel="stylesheet" type="text/css" href="../gui/layout.css" />
    <script type="text/javascript" src="../biz/ext3/adapter/ext/ext-base.js"></script>
	<script type="text/javascript" src="../biz/ext3/ext-all.js"></script>
	<script type="text/javascript" src="../biz/ext3/resources/ext-lang-zh_CN.js"></script>
	<script type="text/javascript" src="../biz/ext3/ux/form/DateTimeField.js"></script>
	<script type="text/javascript" src="../biz/ext3/ux/BizComboBox.js"></script>
	<script type="text/javascript" src="../biz/ext3/ux/RestJson.js"></script>
	<script type="text/javascript" src="../biz/ext3/ux/MultiSorts.js"></script>
	<script type="text/javascript" src="../biz/base/xmlUtil.js"></script>
	<script type="text/javascript" src="../biz/ext3/examples/ux/MultiSelect.js"></script>
	<script type="text/javascript" src="../biz/ext3/examples/ux/ItemSelector.js"></script>
 </head>
<body  style="background-color:rgb(223, 232, 246);">
<div id="form_container"></div>
<div id="grid_container"></div>
<div id="container3" ></div>
<div id="container4"></div>
<div id="container5"></div>
<div id="container6"></div>
<div id="container7"></div>
 <script>
 function initTableGrid(region){
		var selModel=new Ext.grid.CheckboxSelectionModel({singleSelect : false});
		
		var column= new Ext.grid.ColumnModel({
	        columns: [
					{ header: '表名', dataIndex: 'TABLE_NAME', sortable: true },
					{ header: '备注', dataIndex: 'TABLE_COMMENT', sortable: true}
	        ]
	    });
		  
		var store=new Ext.data.JsonStore({
			fields:['TABLE_NAME','TABLE_COMMENT'],
			autoLoad:true,
			proxy:new Ext.data.RestProxy({url:'./list'}),
			
			idProperty:"TABLE_NAME",
			root:"itemList",
			totalProperty: 'pageInfo.total',
			baseParams:{"pageInfo.start":0,"pageInfo.limit":20,"pageInfo.cache":0,"pageInfo.total":20,"sortInfo.field":"TABLE_NAME","sortInfo.direct":"asc"},
			paramNames:{start:'pageInfo.start',limit:'pageInfo.limit',sort:'sortInfo.field',dir:'sortInfo.direct'},
			sortInfo:{
				field : "TABLE_NAME",
				direction : "ASC"
			}
		});
		  var tbars = new Ext.Toolbar({items: [
			  
			  {text:"新建",iconCls:"icon-create",handler:showAddNew},
			  {text:"修改",iconCls:"icon-modify",handler:showModifyWin},
			  {text:"设计",iconCls:"icon-modify",handler:function(){
				  var grid=Ext.getCmp('table_grid'),sels=grid.selModel.getSelections();
				  if(sels.length==0)return;
				  var row=sels[0];
	  			var tabPanel=Ext.getCmp('tab_panel');
	  			tabPanel.showPageAsTab(row.get("TABLE_COMMENT"),row.get("TABLE_NAME"));
			  }},
			  {text:"删除",iconCls:"icon-delete",handler:doDelete},
			  {text:"刷新",iconCls:"icon-finish",handler:function(){
				  store.reload();
			  }},
			  {text:'导入',iconCls:"icon-finish",menu:{
				 items:[
					 {text:'导入元数据表',handler:doImportMeta}
				 ] 
			  }},
			  {text:"导出",iconCls:"icon-finish",menu:{
				  items:[
					  {text:"insert语句",handler:doExportSql},
					  {text:"excel表",handler:doExportExcel},
					  {text:"建表语句",handler:doExportDDL},
				  ]
			  }}
		                          	  ]});
		  var conf = {xtype:'paging',pageSize:20,displayInfo:true,autoWidth:true};
	      conf.autoWidth=true;
	      conf.displayMsg='<font color="red">{0}~{1}</font>条,总数: <font color="red">{2}</font> 条.';
	      conf.emptyMsg= "数据记录为空";
	      conf.store=store;
		  var grid= new Ext.grid.EditorGridPanel({
	          tbar:tbars,
	          title:"数据库表",
	          id:'table_grid',
	          split:true,
	          width:420,
	          store: store,
	          region:region,
	          selModel:selModel,
	          colModel: column,
	          bbar:Ext.create(conf),
	          viewConfig:{forceFit:true}
	      });
		  return grid;
	}


	function initTabPanel(region){
		var tabPanel=new Ext.TabPanel({
	        id:'tab_panel',
	        region:region,
	        split:true,
	        deferredRender:false,
	        enableTabScroll:true,
	        autoScroll: true,
	        autoWidth:false,
	        activeTab:0,
	        bodyCssClass :'bgFilter',
	        items:[]
		});
		tabPanel.layoutTabs={};
		tabPanel.showPageAsTab = function(title,tbName){
			var layoutTabs=tabPanel.layoutTabs;
			if(!title)title=tbName;
			var tid = tbName || ++Ext.Component.AUTO_ID;
			var myTab=layoutTabs[tid];	
			if(!myTab ){
				var path='';
				if( path.substr(0,1)=="."|| path.substr(0,1)=="/"|| path.substr(0,4)=="http"){
					
				}else{
					path = './column.html?table='+tbName;
				}
				var tConf = {title:title, closable:true,autoScroll:true };
					tConf.html='<IFRAME src="'+path+'" frameborder="0" width="100%" height="100%" scrolling="auto" />';
				myTab = tabPanel.add(tConf);
				layoutTabs[tid]=myTab;
				myTab.on("destroy",function(){delete layoutTabs[tid]});
			}
			myTab.show();
			return myTab;
		};
		return tabPanel;
	};


	Ext.onReady(function() {
		Ext.QuickTips.init();
		var tableGrid=initTableGrid('west');
		var tabPanel=initTabPanel('center');
		new Ext.Viewport({
			height : document.documentElement.clientHeight - 10,
			renderTo : 'container7',
	        layout:'border',
	        items : [tableGrid,tabPanel],
	    });
	});

	function showAddNew(){
		var name='addNewWin';
		var xname="addNewForm";
		var win=Ext.getCmp(name);
		if(win) {
			var form=Ext.getCmp(xname).getForm();
			form.reset();
			return win.show();
		}
		var flds=[{
			fieldLabel:"表名",
			xtype:"textfield",
			allowBlank:true,
			name:"table"
		},{
			fieldLabel:"备注",
			name:"comment",
			xtype:"textarea"
		}];
		 var form = new Ext.FormPanel({
		        frame: true,
		        labelAlign: 'right',
		        labelWidth: 85,
		        width:200,
		        id:xname,
		        autoScroll:true,
		        closable:true,
		        waitMsgTarget: true,
		        items: flds,
		        defaults:{
		        	width:190
		        },
		        buttons:[{
	             text:'确认',
	             handler:function(){
	             	var url="./addTable";
	             	var conf={
	 	  	   	  		    clientValidation: true,
	 	  	   	  		    url: url,
	 	  	   	  		    waitMsg:"保存中，请稍后...",
	 	  	   	  		    success: function(form, action) {
	 	  	   		  		  	Ext.getCmp('table_grid').store.reload();
	 	  	   		  	var win=Ext.getCmp(name);
	 	             	win.hide();
	 	  	   	     		},
	 	  	   	  		    failure: function(form, action) {
	 	  	   	  		Ext.getCmp('table_grid').store.reload();
	 	  	   	  		var win=Ext.getCmp(name);
		   	  		    	win.hide();
	 	  	   	  		 		return Ext.Msg.alert("结果","新建失败");
	 	  	   	  		    	
	 	  	   	  		    }
	 	  	   	  		};
	 	  	    	form.getForm().submit(conf);
	             }
	         },{
	             text: '取消',
	             handler: function(){
	             	var win=Ext.getCmp(name);
	             	win.hide();
	             }
	         }]
		 });
		 
		 var win = new Ext.Window({
	         layout:'fit',
	         id:name,
	         scrollable:true,
	         modal:true,
	         title:'新建',
	         width:500,
	         height:280,
	         plain: true,
	         items: form
	     });
		 return  win.show();
	}

	function showModifyWin(){
		var name='modifyWin',xname="modifyForm";
		var win=Ext.getCmp(name);
		if(win) {
			win.loadRecord();
			return win.show();
		}
		var flds=[{
			xtype:"textfield",
			allowBlank:true,
			name:"table",
			readOnly:true,
			hidden:true
		},{
			fieldLabel:"表名",
			xtype:"textfield",
			allowBlank:true,
			name:"name"
		},{
			fieldLabel:"备注",
			name:"comment",
			xtype:"textarea"
		}];
		
		 var form = new Ext.FormPanel({
		        frame: true,
		        labelAlign: 'right',
		        labelWidth: 85,
		        width:200,
		        id:xname,
		        defaults:{
		        	width:190
		        },
		        autoScroll:true,
		        closable:true,
		        waitMsgTarget: true,
		        items: flds,
		        buttons:[{
	             text:'确认',
	             handler:function(){
	             	var url=getContextBase()+"module/DB-updateTable.action";
	             	var conf={
	 	  	   	  		    clientValidation: true,
	 	  	   	  		    url: url,
	 	  	   	  		    waitMsg:"保存中，请稍后...",
	 	  	   	  		    success: function(form, action) {
	 	  	   	  		Ext.getCmp('table_grid').store.reload();
	 	  	   		  	var win=Ext.getCmp(name);
	 	             	win.hide();
	 	  	   	     		},
	 	  	   	  		    failure: function(form, action) {
	 	  	   	  		Ext.getCmp('table_grid').store.reload();
	 	  	   	  		var win=Ext.getCmp(name);
		   	  		    	win.hide();
	 	  	   	  		 		return Ext.Msg.alert("结果","更新失败");
	 	  	   	  		    	
	 	  	   	  		    }
	 	  	   	  		};
	 	  	    	form.getForm().submit(conf);
	             }
	         },{
	             text: '取消',
	             handler: function(){
	             	var win=Ext.getCmp(name);
	             	win.hide();
	             }
	         }]
		 });
		 
		 var win = new Ext.Window({
	         layout:'fit',
	         id:name,
	         scrollable:true,
	         modal:true,
	         title:'编辑',
	         width:500,
	         height:280,
	         plain: true,
	         items: form
	         
	     });
		 var FM={"table":"TABLE_NAME","name":"TABLE_NAME","comment":"TABLE_COMMENT"};
		 win.loadRecord=function(){
			 var records=Ext.getCmp('table_grid').getSelectionModel().getSelections();
			 if(records.length==0)return;
			 var record=records[0];
			 Ext.each(form.items.items,function(f,i){
				 f.setValue(record.json[FM[f["name"]]]);
			 });
		 };
		 win.loadRecord();
		 return  win.show();
	};

	function doExportSql(){
		var arrs = Ext.getCmp("table_grid").selModel.getSelections();
		if(arrs.length<1)return;
		var tbName=arrs[0].get("TABLE_NAME");
		var url="./table/export/insertsql?table="+tbName;
		window.open(url);
	};

	function doExportExcel(){
		var url=getContextBase() +"module/Data-exportExcel.action?jsonStr=";
		var arrs = Ext.getCmp("table_grid").selModel.getSelections();
		var str="";
		for(var i=0;i<arrs.length-1;i++){
			str+=arrs[i].get("TABLE_NAME")+"-";
		}
		str+=arrs[arrs.length-1].get("TABLE_NAME");
		window.open(url+str);	
	};

	function doDelete(){
		var arr = Ext.getCmp("table_grid").selModel.getSelections();
		if(arr.length==0)return;
		var url="./table/delete",tables="",store=Ext.getCmp("table_grid").store;
		for(var i=0;i<arr.length;i++){
			if(i>0){
				tables+=",";
			}
			tables+=arr[i].get("TABLE_NAME");
		}
		Ext.Ajax.request({
			url:url,
			params:{
				tables:tables
			},
			success:function(){
				store.reload();
			},
			failure:function(){
				store.reload();
			}
		});
	};
	
	function doExportDDL(){
		var arrs = Ext.getCmp("table_grid").selModel.getSelections();
		if(arrs.length<1)return;
		var tbName=arrs[0].get("TABLE_NAME");
		var url="./table/export/createsql?table="+tbName;
		window.open(url);
	}

	function doImportMeta(){
		var arrs = Ext.getCmp("table_grid").selModel.getSelections();
		var url=getContextBase()+"/module/DB-importTableMeta.action";
		Ext.each(arrs,function(n,i){
			Ext.Ajax.request({
				url:url,
				params:{
					name:n.get("TABLE_NAME")
				},
				success:function(){
					Ext.Msg.alert('提示','导入成功');
				},
				failure:function(){
					Ext.Msg.alert('提示','导入失败');
				}
			});
		});
	};
 </script>
</body>
</html>


